package club.dataresources.controller;

import club.dataresources.bo.CustomerBo;
import club.dataresources.common.R;
import club.dataresources.entity.Customer;
import club.dataresources.service.CustomerService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author
 * @create 2018-11-12 10:39
 **/
@RequestMapping(value = "/admin/customer")
@Controller
public class AdminCustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/addCustomer")
    public @ResponseBody
    R addCustomer(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "password") String password,
            @RequestParam(value = "phone") String phone,
            @RequestParam(value = "studentStr") String studentStr,
            @RequestParam(value = "memberStr") String memberStr,
            @RequestParam(value = "sexStr") String sexStr,
            @RequestParam(value = "age") int age){
        System.out.println("==================增加新顾客===================");
        try {
            Customer customer = new Customer();
            customer.setName(name);
            customer.setPassword(password);
            customer.setPhone(phone);
            if(studentStr.equals("是")){
                customer.setIsStudent(1);
            }else{
                customer.setIsStudent(0);
            }

            if(memberStr.equals("是")){
                customer.setIsMember(1);
            }else{
                customer.setIsMember(0);
            }
            if(sexStr.equals("是")){
                customer.setSex(1);
            }else{
                customer.setSex(0);
            }
            customer.setAge(age);
            if(customerService.addCustomer(customer)){
                return R.ok().put("code",200).put("msg","新增成功");
            }else{
                return R.error().put("code",500).put("msg","新增失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/removeCustomer")
    public @ResponseBody
    R removeCustomer(@RequestParam("id") Integer id){
        System.out.println("==================删除顾客===================");
        try {
            if(customerService.removeCustomer(id)){
                return R.ok().put("code",200).put("msg","删除成功");
            }else{
                return R.error().put("code",500).put("msg","删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/updateCustomer")
    public @ResponseBody
    R updateCustomer(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "password") String password,
            @RequestParam(value = "phone") String phone,
            @RequestParam(value = "studentStr") String studentStr,
            @RequestParam(value = "memberStr") String memberStr,
            @RequestParam(value = "sexStr") String sexStr,
            @RequestParam(value = "age") int age){
        System.out.println("==================更新顾客===================");
        try {
            Customer customer = new Customer();
            customer.setName(name);
            customer.setPassword(password);
            customer.setPhone(phone);
            customer.setAge(age);
            if(studentStr.equals("是")){
                customer.setIsStudent(1);
            }else{
                customer.setIsStudent(0);
            }

            if(memberStr.equals("是")){
                customer.setIsMember(1);
            }else{
                customer.setIsMember(0);
            }
            if(sexStr.equals("先生")){
                customer.setSex(1);
            }else{
                customer.setSex(0);
            }
            if(customerService.updateCustomer(customer)){
                return R.ok().put("code",200).put("msg","更新成功");
            }else{
                return R.error().put("code",500).put("msg","更新失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }


    @RequestMapping("/listCustomer")
    public @ResponseBody
    R listCustomer(@RequestBody CustomerBo customerBo){
        System.out.println("==================查询顾客===================");
        try {
            Page<Customer> liPage = customerService.listCustomer(customerBo.getPage(),customerBo.getRows(),customerBo);

            List<Customer> liCu = liPage.getResult();
            for(Customer cu:liCu){
                if(cu.getIsStudent().equals(1)){
                    cu.setStudentStr("是");
                }else{
                    cu.setStudentStr("否");
                }

                if(cu.getIsMember().equals(1)){
                    cu.setMemberStr("是");
                }else{
                    cu.setMemberStr("否");
                }

                if(cu.getSex().equals(1)){
                    cu.setSexStr("先生");
                }else{
                    cu.setSexStr("女士");
                }
            }

            return R.ok().put("data",liCu).put("code",200).put("msg","显示完毕").put("total",liPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/adminIndex")
    public @ResponseBody
    R adminIndex(
            HttpServletRequest request,
            HttpServletResponse response){
        System.out.println("===================后台默认显示顾客====================");
        try {
            CustomerBo customerBo = new CustomerBo();
            customerBo.setPage(1);
            customerBo.setRows(10);
            Page<Customer> liPage = customerService.listCustomer(customerBo.getPage(),customerBo.getRows(),customerBo);
            List<Customer> liCu = liPage.getResult();
            for(Customer cu:liCu){
                if(cu.getIsStudent().equals(1)){
                    cu.setStudentStr("是");
                }else{
                    cu.setStudentStr("否");
                }

                if(cu.getIsMember().equals(1)){
                    cu.setMemberStr("是");
                }else{
                    cu.setMemberStr("否");
                }

                if(cu.getSex().equals(1)){
                    cu.setSexStr("先生");
                }else{
                    cu.setSexStr("女士");
                }
            }
            return R.ok().put("data",liCu).put("code",200).put("msg","显示完毕").put("total",liPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }
}
